//
// Created by yonghu0 on 25-10-9.
//

#include <iostream>
#include <set> //multiset所在头文件
#include <functional> //greater的头文件
#include <utility> //pair的头文件

void print(std::multiset<int> &my_multiset)
{
    for (auto &elem: my_multiset)
    {
        std::cout << elem << " ";
    }
    std::cout << std::endl;
}

int main()
{
    std::cout << "multiset_test begin" << std::endl;

    std::cout << "--------------创建multiset----------------" << std::endl;
    //创建递增排序的multiset
    //方法一
    std::multiset<int> my_multiset1;
    //方法二
    int arr1[] = {3, 1, 4, 2};
    std::multiset<int> my_multiset2(arr1, arr1 + 4);
    for (auto &elem: my_multiset2)
    {
        std::cout << elem << " ";
    }
    std::cout << std::endl;
    //创建递减排序的multiset
    std::multiset<int, std::greater<int> > my_multiset3;
    my_multiset3.insert(arr1, arr1 + 4);
    for (auto &elem: my_multiset3)
    {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    std::cout << "----------------容器size--------------" << std::endl;
    std::cout << my_multiset1.size() << std::endl;
    std::cout << "----------------容器判空---------------" << std::endl;
    std::cout << my_multiset1.empty() << std::endl;

    std::cout << "----------插入元素--------------------" << std::endl;
    //插入单个元素
    std::multiset<int>::iterator res = my_multiset1.insert(5);
    print(my_multiset1);
    std::cout << *res << std::endl;
    res = my_multiset1.insert(5); //插入重复元素
    print(my_multiset1);
    std::cout << *res << std::endl;
    //插入区间
    my_multiset1.insert(arr1, arr1 + 4);
    print(my_multiset1);

    std::cout << "------------查找元素(判断元素是否在容器中)-----------" << std::endl;
    //方法一
    if (my_multiset1.find(3) != my_multiset1.end())
    {
        std::cout << "元素在multiset中" << std::endl;
    } else
    {
        std::cout << "元素不在multiset中" << std::endl;
    }
    //方法二
    if (my_multiset1.count(3))
    {
        std::cout << "元素在multiset中" << std::endl;
    } else
    {
        std::cout << "元素不在multiset中" << std::endl;
    }

    std::cout << "------------修改multiset中的元素(错误操作)--------------" << std::endl;
    //multiset的迭代器是const_iterator，multiset中的元素不允许修改，
    //因为修改的话会引起底层红黑树的结构变化

    std::cout << "------------删除元素-----------------------------" << std::endl;
    //删除单个元素
    //方法一
    my_multiset1.erase(5);
    print(my_multiset1);
    my_multiset1.erase(11); //删除不存在的元素，不报错
    print(my_multiset1);
    //方法二
    my_multiset1.erase(my_multiset1.begin());
    print(my_multiset1);
    //删除区间
    //用的不多，这里就不列举

    std::cout << "----其他用法(lower_bound/upper_bound/equal_range)-----" << std::endl;
    //lower_bound/upper_bound/equal_range方法
    std::set<int>::iterator it_low_bound = my_multiset1.lower_bound(3);
    std::cout << *it_low_bound << std::endl;
    std::set<int>::iterator it_upper_bound = my_multiset1.upper_bound(3);
    std::cout << *it_upper_bound << std::endl;
    std::pair<std::set<int>::iterator, std::set<int>::iterator> p_equal_range;
    p_equal_range = my_multiset1.equal_range(3);
    std::cout << *(p_equal_range.first) << " " << *(p_equal_range.second) << std::endl;

    std::cout << "------------清空容器-----------------" << std::endl;
    my_multiset1.clear();
    print(my_multiset1);

    std::cout << "multiset_test end" << std::endl;
    return 0;
}
